package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * Class used to build menus for the game display's frame
 */
public class MenuBuilder implements MenuManager {
	
	// public methods
		
	/**
	 * Builds a menu-bar for new game.
	 * Uses game-mode-manager object while pairing each button with a listener 
	 */
	@Override
	public JMenuBar buildNewGameMenu(SetGameModes gameModeMngr) {
		
		// create a new menu-bar object
		JMenuBar menuBar = new JMenuBar();

		// create a new menu object and set its title
		JMenu menu = new JMenu("New Game");
		
		JMenuItem item = null;
		
		// create Single Player item
		item = new JMenuItem("Two Players");
		
		final SetGameModes setGameModes = gameModeMngr;
		
		// create listener for New Game item
		ActionListener twoPlayerListener = new ActionListener() {			
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// set two players game
				setGameModes.setTwoPlayersGame();
			}
		};
		
		// pair item with listener
		item.addActionListener(twoPlayerListener);
		// add item to menu
		menu.add(item);

		// create Single Player item		
		item = new JMenuItem("Single Player");
		
		// create listener for New Game item
		ActionListener singlePlayerListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// set single player game
				setGameModes.setSinglePlayerGame();
			}
		};
		
		// pair item with listener
		item.addActionListener(singlePlayerListener);
		// add item to menu
		menu.add(item);
		
		// create Automatic Game item
		item = new JMenuItem("Automatic Game");
		
		// create listener for New Game item
		ActionListener automaticGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// set automatic game
				setGameModes.setAutomaticGame();
			}
		};
		
		// pair item with listener
		item.addActionListener(automaticGameListener);
		// add item to menu
		menu.add(item);
		
		// create Partial Game item
		item = new JMenuItem("Partial Game");
		
		// create listener for New Game item
		ActionListener partialGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// set partial game
				setGameModes.setPartialGameBoard();
			}
		};
		
		// pair item with listener
		item.addActionListener(partialGameListener);		
		// add item to menu
		menu.add(item);
		
		// create Partial Game item
		item = new JMenuItem("Special Game");
		
		// create listener for New Game item
		ActionListener specialGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// set special game
				setGameModes.setSpecialGame();
			}
		};
		
		// pair item with listener
		item.addActionListener(specialGameListener);		
		// add item to menu
		menu.add(item);
		
		// add menu to the menu bar
		menuBar.add(menu);
		
		return menuBar;
	}
}